home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / Send the Hack / Hack INIT.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-18  |  7.9 KB  |  394 lines  |  [TEXT/KAHL]

  1. #include <SetUpA4.h>
  2.  
  3. #define        SystemTaskTrap                                                            0xA9B4
  4. #define        SystemMenuTrap                                                            0xA9B5
  5.  
  6. #define        aeSelectionKeyword                                                        'fsel'
  7. #define        aeOpenSelection                                                            'sope'
  8. #define        kFinderSig                                                                    'FNDR'
  9. #define        kSystemType                                                                'MACS'
  10.  
  11. #define        SaveRegs()                    asm {MOVEM.L    A0-A5/D0-D7, -(A7)}
  12. #define        RestoreRegs()                asm {MOVEM.L    (A7)+, A0-A5/D0-D7}
  13.  
  14.  
  15.                         void            main (void);
  16.             pascal    void            newSystemTask (void);
  17.             pascal    void            newSystemMenu (long);
  18.                         void            LaunchItems (short);
  19.                         OSErr        FindAProcess (OSType, OSType, ProcessSerialNumberPtr);
  20.                         void            AddFinderMenu (void);
  21. extern    pascal    void            ShowINIT (short, short);
  22.  
  23.  
  24. MenuHandle            theMenuResHndl=nil;
  25. Ptr                        oldSystemTask;
  26. Ptr                        oldSystemMenu;
  27. short                    theMenuResID=-13;
  28.  
  29.  
  30.  
  31. void main ()
  32. {
  33.     Handle        initHndl=nil;
  34.     Ptr            initPtr;
  35.     
  36.     asm
  37.     {
  38.         move.l    a0, initPtr
  39.     }
  40.     
  41.     
  42.     RememberA0();
  43.     SetUpA4();
  44.     
  45.     initHndl=RecoverHandle (initPtr);
  46.     DetachResource (initHndl);
  47.  
  48.     AddFinderMenu ();
  49.     
  50.     oldSystemTask=StripAddress ((Ptr) NGetTrapAddress (SystemTaskTrap, ToolTrap));
  51.     NSetTrapAddress ((long) newSystemTask, SystemTaskTrap, ToolTrap);
  52.  
  53.     ShowINIT (256, -1);
  54.  
  55.     RestoreA4();
  56. }
  57.  
  58.  
  59. pascal void newSystemTask ()
  60. {
  61.                 Handle        mdefHndl;
  62.                 Ptr            oldSystemTaskPtr;
  63.     static    Boolean    flagWNE=true;
  64.                 Boolean    finder;
  65.  
  66.     SaveRegs();
  67.     SetUpA4();
  68.  
  69.     oldSystemTaskPtr=oldSystemTask;
  70.  
  71.     finder=EqualString (FinderName, CurApName, false, false);
  72.     
  73.     if (finder)
  74.     {        
  75.         if (flagWNE)
  76.         {
  77.             flagWNE=false;
  78.  
  79.             mdefHndl=GetResource ('MDEF', 0);
  80.             (**theMenuResHndl).menuProc=mdefHndl;
  81.             CalcMenuSize (theMenuResHndl);
  82.             InsertMenu (theMenuResHndl, 0);
  83.             DrawMenuBar ();
  84.             
  85.             oldSystemMenu=StripAddress ((Ptr) NGetTrapAddress (SystemMenuTrap, ToolTrap));
  86.             NSetTrapAddress ((long) newSystemMenu, SystemMenuTrap, ToolTrap);
  87.         }
  88.     }
  89.     
  90.     RestoreA4();
  91.     RestoreRegs();
  92.     
  93.     asm
  94.     {
  95.         move.l            oldSystemTaskPtr, a0
  96.         unlk                a6
  97.         jmp                (a0)
  98.     }
  99. }
  100.  
  101.  
  102.  
  103. pascal void newSystemMenu (long menuResult)
  104. {
  105.     Ptr                oldSystemMenuPtr;
  106.     Boolean        test;
  107.     short            menuID, menuItem;
  108.     short            appleMenuItem=1, controlPanelsItem=2, extensionsItem=3, preferencesItem=4, startupItem=5;
  109.  
  110.     SaveRegs();
  111.     SetUpA4();
  112.  
  113.     oldSystemMenuPtr=oldSystemMenu;
  114.  
  115.     menuID=HiWord (menuResult);
  116.     menuItem=LoWord (menuResult);
  117.     
  118.     test=EqualString (FinderName, CurApName, false, false);
  119.  
  120.     if (test)
  121.     {
  122.         if (menuID == theMenuResID)
  123.         {
  124.             if (menuItem == appleMenuItem || menuItem == controlPanelsItem || menuItem == extensionsItem ||
  125.                 menuItem == preferencesItem || menuItem == startupItem)
  126.             {
  127.                 menuResult=0;
  128.                 LaunchItems (menuItem);
  129.             }
  130.             
  131.         }
  132.     }
  133.  
  134.     RestoreA4();
  135.     RestoreRegs();
  136.  
  137.     asm
  138.     {
  139.         move.l        oldSystemMenuPtr, a0
  140.         unlk            a6
  141.         jmp            (a0)
  142.     }
  143. }
  144.  
  145.  
  146.  
  147.  
  148. void LaunchItems (short itemNum)
  149. {
  150.     OSErr                                err;
  151.     ProcessSerialNumber        process;
  152.     AppleEvent                        aeEvent;
  153.     AEDesc                                theAddressDesc, aeDirDesc, fileList, listElem;
  154.     FSSpec                                theDocToOpen, dirSpec;
  155.     CInfoPBRec                        dirBlock, fileBlock;
  156.     AliasHandle                    dirAliasHndl=nil, fileAliasHndl=nil;
  157.     Str255                                dirName, fileName, errStrng2, errStrng3;
  158.     Boolean                            test;
  159.     long                                    foundDirID, parID, theFolderType;
  160.     short                                i, j, defaultVol=0, numFiles, foundVRefNum;
  161.  
  162.     SetUpA4();
  163.     
  164.     switch (itemNum)
  165.     {
  166.         case    1:
  167.                 theFolderType=kAppleMenuFolderType;
  168.                 break;
  169.         case    2:
  170.                 theFolderType=kControlPanelFolderType;
  171.                 break;
  172.         case    3:
  173.             theFolderType=kExtensionFolderType;
  174.                 break;
  175.         case    4:
  176.             theFolderType=kPreferencesFolderType;
  177.                 break;
  178.         case    5:
  179.             theFolderType=kStartupFolderType;
  180.                 break;
  181.     }
  182.     
  183.     err=FindFolder (defaultVol, theFolderType, kOnSystemDisk, &foundVRefNum, &foundDirID);
  184.     
  185.     if (err != noErr)
  186.         goto Exit;
  187.     
  188.     dirBlock.dirInfo.ioCompletion=nil;
  189.     dirBlock.dirInfo.ioNamePtr=dirName;
  190.     dirBlock.dirInfo.ioVRefNum=defaultVol;
  191.     dirBlock.dirInfo.ioFDirIndex=-1;
  192.     dirBlock.dirInfo.ioDrDirID=foundDirID;
  193.     
  194.     err=PBGetCatInfoSync (&dirBlock);
  195.  
  196.     if (err != noErr)
  197.         goto Exit;
  198.     
  199.     numFiles=dirBlock.dirInfo.ioDrNmFls;
  200.     
  201.     if (numFiles > 0)
  202.     {
  203.         parID=dirBlock.dirInfo.ioDrParID;
  204.             
  205.         fileBlock.hFileInfo.ioCompletion=nil;
  206.         fileBlock.hFileInfo.ioNamePtr=fileName;
  207.         fileBlock.hFileInfo.ioVRefNum=defaultVol;
  208.         fileBlock.hFileInfo.ioFDirIndex=1;
  209.         fileBlock.hFileInfo.ioDirID=foundDirID;
  210.         
  211.         err=PBGetCatInfoSync (&fileBlock);
  212.     
  213.         if (err != noErr)
  214.             goto Exit;
  215.     
  216.         test=FindAProcess (kFinderSig, kSystemType, &process);
  217.                 
  218.         theAddressDesc.descriptorType=0;
  219.         theAddressDesc.dataHandle=nil;
  220.         err=AECreateDesc (typeProcessSerialNumber, &process, sizeof (process), &theAddressDesc);
  221.         
  222.         if (err != noErr)
  223.             goto Exit;;
  224.         
  225.         err=AECreateAppleEvent (kFinderSig, aeOpenSelection, &theAddressDesc,
  226.                                                     kAutoGenerateReturnID, kAnyTransactionID, &aeEvent);
  227.     
  228.         if (err != noErr)
  229.             goto Exit;;
  230.     
  231.         theDocToOpen.vRefNum=defaultVol;
  232.         theDocToOpen.parID=foundDirID;
  233.         
  234.         for (j=0;j<=fileBlock.hFileInfo.ioNamePtr[0];j++)
  235.             theDocToOpen.name[j]=fileBlock.hFileInfo.ioNamePtr[j];
  236.     
  237.         err=FSMakeFSSpec (theDocToOpen.vRefNum, theDocToOpen.parID, nil, &dirSpec);
  238.     
  239.         if (err != noErr)
  240.             goto Exit;
  241.     
  242.         err=NewAlias (nil, &dirSpec, &dirAliasHndl);
  243.     
  244.         if (err != noErr)
  245.             goto Exit;
  246.     
  247.         HLock (dirAliasHndl);
  248.         
  249.         aeDirDesc.descriptorType=0;
  250.         aeDirDesc.dataHandle =nil;
  251.         err=AECreateDesc (typeAlias, *dirAliasHndl, GetHandleSize (dirAliasHndl), &aeDirDesc);
  252.     
  253.         if (err != noErr)
  254.             goto Exit;
  255.     
  256.         HUnlock (dirAliasHndl);
  257.         DisposHandle (dirAliasHndl);
  258.     
  259.         err=AEPutParamDesc(&aeEvent, keyDirectObject, &aeDirDesc);
  260.         
  261.         if (err != noErr)
  262.             goto Exit;
  263.         
  264.         err=AEDisposeDesc (&aeDirDesc);
  265.     
  266.         if (err != noErr)
  267.             goto Exit;
  268.     
  269.         fileList.descriptorType=0;
  270.         fileList.dataHandle=nil;
  271.         err=AECreateList (nil, 0, false, &fileList);
  272.     
  273.         if (err != noErr)
  274.             goto Exit;
  275.     
  276.         for (i=1;i<=numFiles;i++)
  277.         {
  278.             fileBlock.hFileInfo.ioFDirIndex=i;
  279.             fileBlock.hFileInfo.ioDirID=foundDirID;
  280.             
  281.             err=PBGetCatInfoSync (&fileBlock);
  282.     
  283.             if (err != noErr)
  284.                 goto Exit;
  285.     
  286.             theDocToOpen.vRefNum=defaultVol;
  287.             theDocToOpen.parID=foundDirID;
  288.             
  289.             for (j=0;j<=fileBlock.hFileInfo.ioNamePtr[0];j++)
  290.                 theDocToOpen.name[j]=fileBlock.hFileInfo.ioNamePtr[j];
  291.         
  292.             err=FSMakeFSSpec(theDocToOpen.vRefNum, theDocToOpen.parID, nil, &dirSpec);
  293.     
  294.             if (err != noErr)
  295.                 goto Exit;
  296.     
  297.             err=NewAlias (nil, &theDocToOpen, &fileAliasHndl);
  298.             
  299.             if (err != noErr)
  300.                 goto Exit;
  301.             
  302.             HLock (fileAliasHndl);
  303.             err=AECreateDesc (typeAlias, *fileAliasHndl, GetHandleSize (fileAliasHndl), &listElem);
  304.         
  305.             if (err != noErr)
  306.                 goto Exit;
  307.     
  308.             HUnlock (fileAliasHndl);
  309.             DisposHandle (fileAliasHndl);
  310.             
  311.             err=AEPutDesc (&fileList, i, &listElem);
  312.         
  313.             if (err != noErr)
  314.                 goto Exit;
  315.         
  316.             err=AEDisposeDesc(&listElem);
  317.         
  318.             if (err != noErr)
  319.                 goto Exit;
  320.     
  321.             err=AEPutParamDesc (&aeEvent, aeSelectionKeyword, &fileList);
  322.             
  323.             if (err != noErr)
  324.                 goto Exit;
  325.         }
  326.     
  327.         err=AEDisposeDesc (&fileList);
  328.         
  329.         if (err != noErr)
  330.             goto Exit;
  331.         
  332.         err=AESend (&aeEvent, nil, kAENoReply + kAEAlwaysInteract + kAECanSwitchLayer,
  333.                             kAEHighPriority, kAEDefaultTimeout, nil, nil);
  334.     
  335.         if (err != noErr)
  336.             goto Exit;
  337.     
  338.         err=AEDisposeDesc (&aeEvent);
  339.     }
  340.  
  341. Exit:
  342.  
  343.     if (err != noErr)
  344.         DebugStr ("\pAn Error has occurred");
  345.     
  346.     RestoreA4();
  347. }
  348.  
  349.  
  350. OSErr FindAProcess (OSType typeToFind, OSType creatorToFind, ProcessSerialNumberPtr processSN)
  351. {
  352.     ProcessInfoRec tempInfo;
  353.     FSSpec procSpec;
  354.     Str31 processName;
  355.     OSErr err = noErr;
  356.  
  357.     processSN->lowLongOfPSN = kNoProcess;
  358.     processSN->highLongOfPSN = kNoProcess;
  359.  
  360.     tempInfo.processInfoLength = sizeof(ProcessInfoRec);
  361.     tempInfo.processName = (StringPtr) &processName;
  362.     tempInfo.processAppSpec = &procSpec;
  363.  
  364.     do
  365.     {
  366.         err = GetNextProcess(processSN);
  367.     
  368.         if (err == noErr)
  369.             GetProcessInformation(processSN, &tempInfo);
  370.             
  371.     }while ((tempInfo.processSignature != creatorToFind || tempInfo.processType != typeToFind) || err != noErr);
  372.  
  373.     return(err);
  374. }
  375.  
  376.  
  377.  
  378. void AddFinderMenu ()
  379. {    
  380.     SetUpA4();
  381.  
  382.     theMenuResHndl=(MenuHandle) Get1IndResource ('MENU', 1);
  383.     HLock (theMenuResHndl);
  384.     DetachResource (theMenuResHndl);
  385.  
  386.     RestoreA4();
  387. }
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.